<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style type="text/css">
        .el-main{
            position: absolute;
            top: 70px;
            bottom: 0px;
            left: 200px;
            right: 10px;
            padding: 0;
        }
    </style>
</head>
<body class="hold-transition skin-purple sidebar-mini">
<div id="app">
    <el-container>
        <el-header  class="main-header" style="height:70px;">
            <nav class="navbar navbar-static-top" :class=''>
                <!-- Logo -->
                <a href="#" class="logo" style="text-align:center">
                    <span class="logo-lg"><img src="../img/logo.png"></span>
                </a>
                <div class="right-menu">
                    <span class="help"><i class="fa fa-exclamation-circle" aria-hidden="true"></i>帮助</span>
                    <el-dropdown class="avatar-container right-menu-item" trigger="click">
                        <div class="avatar-wrapper">
                            <img src="../img/user2-160x160.jpg" class="user-avatar">
                            {{username}}
                        </div>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item divided>
                                <span style="display:block;" @click="handleEditPwd()">修改密码</span>
                            </el-dropdown-item>
                            <el-dropdown-item divided>
                                <span style="display:block;" @click="handleCreate()">修改个人信息</span>
                            </el-dropdown-item>
                            <el-dropdown-item divided>
                                <span style="display:block;"><a href="/logout.do">退出</a></span>
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </div>
            </nav>
        </el-header>
        <el-container>
            <el-aside width="200px">
                <el-menu>
                    <el-submenu v-for="menu in menuList" :index="menu.path">
                        <template slot="title">
                            <i class="fa" :class="menu.icon"></i>
                            {{menu.title}}
                        </template>
                        <template v-for="child in menu.children">
                            <el-menu-item :index="child.path">
                                <a :href="child.linkUrl" target="right">{{child.name}}</a>
                            </el-menu-item>
                        </template>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-container>
                <iframe name="right" class="el-main" src="ordersetting.html" width="100%" height="580px" frameborder="0"></iframe>
            </el-container>
            
            <!--弹出修改密码-->
            <el-form :model="pwdForm" status-icon :rules="rules" ref="pwdForm" label-width="100px" class="demo-pwdForm">
                <el-dialog title="修改个人密码" :visible.sync="dialogPwdFormVisible4Edit">
                <el-form-item label="旧密码" prop="oldPass">
                    <el-input  type="password" v-model.number="pwdForm.oldPass"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="pass">
                    <el-input type="password" v-model="pwdForm.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="pwdForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('pwdForm')">提交</el-button>
                    <el-button @click="resetPwdForm('pwdForm')">重置</el-button>
                </el-form-item>
                </el-dialog>
            </el-form>
            
            <!--弹出修改个人信息-->
            <div class="add-form">
                <el-dialog title="修改个人信息" :visible.sync="dialogFormVisible4Edit">
                    <el-form :model="ruleForm"  ref="ruleForm" label-width="100px" class="demo-ruleForm"><!--status-icon :rules="rules"-->
                        <el-form-item label="用户名" prop="username">
                            <el-input type="username" readonly="true" v-model="ruleForm.username" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="性别" prop="gender">
                            <el-select v-model="ruleForm.gender">
                                <el-option label="男" value="1"></el-option>
                                <el-option label="女" value="2"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="电话号" prop="telephone">
                            <el-input type="remark" v-model="ruleForm.telephone" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="出生日期" prop="birthday">
                            <!--<el-input v-model.number="ruleForm.birthday"></el-input>-->
                            <div class="block">
                                <!--<span class="demonstration">出生日期</span>-->
                                <el-date-picker
                                        v-model="ruleForm.birthday"
                                        type="date"
                                        placeholder="选择日期"><!--:formatter="formatDate"-->
                                </el-date-picker>
                            </div>
                        </el-form-item>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="resetForm()">重置</el-button>
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>

                </el-dialog>
            </div>


        </el-container>

    </el-container>

</div>

</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            var checkoldPass = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('旧密码不能为空'));
                }else {
                    callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.pwdForm.checkPass !== '') {
                        this.$refs.pwdForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.pwdForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                pwdForm: {
                    oldPass: '',
                    pass: '',
                    checkPass: ''

                },
                rules: {
                    oldPass: [
                        { validator: checkoldPass, trigger: 'blur' }
                        // ,
                        // { min: 3, message: "密码长度不小于3个字符", trigger: "blur" },
                    ],
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                        // ,
                        // { min: 4, message: "密码长度不小于4个字符", trigger: "blur" },
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                        // ,
                        // { min: 4, message: "密码长度不小于4个字符", trigger: "blur" },

                    ]

                },
                ruleForm:{},//个人信息框
                // pwdForm:{},//修改密码框
                username:null,
                menuList:[],
                formData:[],//表单数据
                dialogFormVisible4Edit:false,
                dialogPwdFormVisible4Edit:false

            };
        },
        methods: {
            submitForm(pwdFrom) {
                this.$refs[pwdFrom].validate((valid) => {
                    if (valid) {

                        this.pwdForm.username = this.username;
                        //异步请求 更改密码.
                        axios.post("/user/editPass.do",this.pwdForm).then((res) => {
                            if(res.data.flag){
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                                this.dialogPwdFormVisible4Edit = false;
                                this.resetPwdForm(pwdFrom);
                            }else{
                                this.$message.error(res.data.message);
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetPwdForm(formName) {
                this.$refs[formName].resetFields();
            },
            //修改个人密码方法
            handleEditPwd(){
                this.dialogPwdFormVisible4Edit = true;
            },
            //修改个人信息方法
            handleCreate() {
                this.resetForm();
                //将修改个人信息显示出来
                // 弹出编辑窗口
                this.dialogFormVisible4Edit = true;
                //回显数据
                axios.get("/user/findByCondition.do?username=" + this.username).then((res) => {
                    if(res.data.flag){
                        this.ruleForm = res.data.data;//为模型数据赋值，基于VUE数据绑定进行回显
                    }else{
                        this.$message.error(res.data.message);
                    }
                });
            },
            //重置数据
            resetForm(){
                this.ruleForm = {};
            },
            //编辑个人信息方法
            handleEdit() {
                //进行表单校验
                this.$refs['ruleForm'].validate((valid) => {
                    if(valid){
                        //如果校验通过，发送ajax请求，将表单数据提交到Controller

                        axios.post("/user/editUser.do",this.ruleForm).then((res) => {
                            if(res.data.flag){
                                //处理成功
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                                //关闭新增窗口
                                this.dialogFormVisible4Edit = false;
                            }else{
                                //处理失败
                                this.$message.error(res.data.message);
                            }
                        });
                    }else{
                        //校验不通过，提示错误信息
                        this.$message.error("表单数据校验失败，请检查输入是否正确！");
                    }
                });
            },


        },
        created(){
            //发送ajax请求，获取当前登录用户的用户名，用于页面展示
            axios.get("/user/getLoginUsername.do").then((res) => {
                this.username = res.data.data;
                axios.get("/menu/findMenuList.do?username="+this.username).then((res)=>{
                    this.menuList = res.data.data;
                })
            });
            /*axios.get("/menu/findMenuList.do?username="+this.username).then((res)=>{
                this.menuList = res.data.data;
            })*/
        }
    });

    $(function() {
        var wd = 200;
        $(".el-main").css('width', $('body').width() - wd + 'px');
    });

</script>
</html>
